/*
 * parser.cpp
 * This file is part of OpenAnimator
 *
 * Copyright (C) 2008 - Abhinav Lele
 *
 * OpenAnimator is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenAnimator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenAnimator; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */
 
 
#include "parser.h"
#include "xlua.h"

/*
const char Parser::className[] = "Parser";

#define method(class, name) {#name, &class::name}

Lunar<Parser>::RegType Parser::methods[] = {
  method(Parser, get_mapping),
  method(Parser, set_mapping),
  {0,0}
};
*/
Parser::Parser(string mappingscriptpath) {
	
	
	script_land.load_library(LUALIB_BASE);	 script_land.load_library(LUALIB_TABLE);
	script_land.load_library(LUALIB_STRING); script_land.load_library(LUALIB_MATH);
	script_land.load_library(LUALIB_XML);
//    Lunar<Parser>::Register(script_land.L);
//	int parser_instance = Lunar<Parser>::push(script_land.L,this); // push and get location on stack
//	cout << "parser_instance : " << parser_instance << endl;
	
//	lua_pushliteral(script_land.L,"reader");
//	lua_pushvalue(script_land.L,parser_instance);
//	lua_settable(script_land.L,LUA_GLOBALSINDEX);
//	
	/*register Parser:set_mapping and Parser::get_mapping */
	
	script_land.create_link(XLUA_parser_get_mapping,"get_mapping");
	script_land.create_link(XLUA_parser_set_mapping,"set_mapping");
	
	script_land.create_link(XLUA_library_new,"library_new");
	script_land.create_link(XLUA_library_get,"library_get");
	script_land.create_link(XLUA_library_set,"library_set");
	script_land.create_link(XLUA_library_del,"library_del");
	script_land.create_link(XLUA_canvas_new,"canvas_new");
	script_land.create_link(XLUA_canvas_get,"canvas_get");
	script_land.create_link(XLUA_canvas_set,"canvas_set");
	script_land.create_link(XLUA_canvas_del,"canvas_del");
	script_land.create_link(XLUA_config_new,"config_new");
	script_land.create_link(XLUA_config_get,"config_get");
	script_land.create_link(XLUA_config_set,"config_set");
	script_land.create_link(XLUA_config_del,"config_del");
	script_land.create_link(UTIL_getuniqnumber,"getuniqnumber");
    script_land.create_link(UTIL_getos,"getos");
	script_land.create_link(XLUA_loadimage,"loadimage");
	script_land.create_link(EVENT_setevent,"event_set");
	
	script_land.load_file(mappingscriptpath.c_str());
	script_land.call("init");
	script_land.remove_link(string("init"));
	script_land.remove_link(string("set_mapping"));
	script_land.remove_link(string("get_mapping"));
	/* tag fn map initialization done */

	
	
	/*
	*/
	/* ready for parsing */
}



void Parser::read(const char *filepath) {
	T("parser:read: " << filepath)
	TiXmlDocument doc(filepath);
	TiXmlNode * node;
	TiXmlElement *todoElement,*element;
	string typeAttr;
	
	map<string,string,ltstr_stl>::iterator iter;
	doc.LoadFile(TIXML_ENCODING_UTF8);
	node = doc.RootElement();
	if(!node) { E("No root element!") return; }

	if(strcmp(node->Value(),"openanimator") != 0) { E("parser:read:  openanimator root node not found") return; }
	M("parser:read: openanimator node found")
	todoElement = node->ToElement();

	for(element = todoElement->FirstChildElement(); element ; element = element->NextSiblingElement() )  {
        TiXmlPrinter xmlPrinter;
		M("parser:read: node = " << element->Value() )
		if(strcmp(element->Value(),"silo") != 0) continue;
		typeAttr = string(element->Attribute("type"));
		iter = mapping_tag_fn.find(typeAttr);
		if(iter == mapping_tag_fn.end()) { E("Parser::read : tag mapping not found. tag = " << typeAttr << endl) continue; }
		element->Accept(&xmlPrinter);
		script_land.call(iter->second,xmlPrinter.CStr());

	}
}
